﻿/*----------------------------------------------------------------
// Copyright (C) 2012
// 版权所有
//
// 文件名：OrderOnline_Role.cs
// 文件功能描述：
//
//
// 创建标识：Libing 2012-05-10 00:53:14 星期四
//
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using WMS.DBUtility;

namespace WMS.SQLServerDAL
{
    public class OrderOnline_Role : WMS.IDAL.IOrderOnline_Role
    {
        #region 基本方法

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="orderOnlineRole">类型为 WMS.Model.OrderOnline_Role 的实体</param>
        /// <returns></returns>
        public bool Insert(WMS.Model.OrderOnline_Role orderOnlineRole)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.NVarChar, 36),
				new SqlParameter("@RoleName", SqlDbType.NVarChar, 50),
				new SqlParameter("@RoleDescr", SqlDbType.NText),
				new SqlParameter("@Rights", SqlDbType.VarChar),
				new SqlParameter("@RoleType", SqlDbType.Int, 4),
				new SqlParameter("@RoleShop", SqlDbType.VarChar, 100)
			};
            parameters[0].Value = orderOnlineRole.Id;
            parameters[1].Value = orderOnlineRole.RoleName;
            parameters[2].Value = orderOnlineRole.RoleDescr;
            parameters[3].Value = orderOnlineRole.Rights;
            parameters[4].Value = orderOnlineRole.RoleType;
            parameters[5].Value = orderOnlineRole.RoleShop;

            try
            {
                int result = DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Role_Insert]", parameters);

                return result > 0;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="orderOnlineRole">类型为 WMS.Model.OrderOnline_Role 的实体</param>
        /// <returns></returns>
        public bool Update(WMS.Model.OrderOnline_Role orderOnlineRole)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.VarChar, 36),
				new SqlParameter("@RoleName", SqlDbType.NVarChar, 50),
				new SqlParameter("@RoleDescr", SqlDbType.NText),
				new SqlParameter("@Rights", SqlDbType.VarChar),
				new SqlParameter("@RoleType", SqlDbType.Int, 4),
				new SqlParameter("@RoleShop", SqlDbType.VarChar, 100)
			};
            parameters[0].Value = orderOnlineRole.Id;
            parameters[1].Value = orderOnlineRole.RoleName;
            parameters[2].Value = orderOnlineRole.RoleDescr;
            parameters[3].Value = orderOnlineRole.Rights;
            parameters[4].Value = orderOnlineRole.RoleType;
            parameters[5].Value = orderOnlineRole.RoleShop;

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Role_Update]", parameters) > 0;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 最小修改实体
        /// </summary>
        /// <param name="model">类型为WMS.Model.OrderOnline_Role的实体</param>
        /// <returns></returns>
        public bool UpdateMinimal(WMS.Model.OrderOnline_Role orderOnlineRole)
        {
            SqlCommand cmd = new SqlCommand("[OrderOnline_Role_UpdateMinimal]");

            if (!String.IsNullOrEmpty(orderOnlineRole.Id))
            {
                cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar, 36)).Value = orderOnlineRole.Id;
            }

            if (!String.IsNullOrEmpty(orderOnlineRole.RoleName))
            {
                cmd.Parameters.Add(new SqlParameter("@RoleName", SqlDbType.NVarChar, 50)).Value = orderOnlineRole.RoleName;
            }

            if (!String.IsNullOrEmpty(orderOnlineRole.RoleDescr))
            {
                cmd.Parameters.Add(new SqlParameter("@RoleDescr", SqlDbType.NText)).Value = orderOnlineRole.RoleDescr;
            }

            if (!String.IsNullOrEmpty(orderOnlineRole.Rights))
            {
                cmd.Parameters.Add(new SqlParameter("@Rights", SqlDbType.VarChar, -1)).Value = orderOnlineRole.Rights;
            }

            if (orderOnlineRole.RoleType != null)
            {
                cmd.Parameters.Add(new SqlParameter("@RoleType", SqlDbType.Int, 4)).Value = orderOnlineRole.RoleType;
            }

            if (!String.IsNullOrEmpty(orderOnlineRole.RoleShop))
            {
                cmd.Parameters.Add(new SqlParameter("@RoleShop", SqlDbType.VarChar, 100)).Value = orderOnlineRole.RoleShop;
            }

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery(cmd) > 0;
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 删除实体(根据主键 ID)
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns></returns>
        public bool DeleteByID(string id)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.VarChar,36)
			};
            parameters[0].Value = id;

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Role_DeleteByID]", parameters) > 0;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 删除实体(指定条件)
        /// </summary>
        /// <param name="whereClause">WHERE语句</param>
        /// <returns></returns>
        public bool DeleteDynamic(string whereClause)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@WhereClause", SqlDbType.NVarChar,2000)
			};
            parameters[0].Value = whereClause;

            try
            {
                return DbHelperSQL.ExecuteStoredProcNonQuery("[OrderOnline_Role_DeleteDynamic]", parameters) > 0;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 获取实体(根据主键 ID)
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns></returns>
        public WMS.Model.OrderOnline_Role GetByID(string id)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@ID", SqlDbType.VarChar,36)
			};
            parameters[0].Value = id;

            try
            {
                List<WMS.Model.OrderOnline_Role> orderOnlineRoleList = new List<WMS.Model.OrderOnline_Role>();

                SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_Role_GetByID]", parameters);
                while (dr.Read())
                {
                    WMS.Model.OrderOnline_Role orderOnlineRole = new WMS.Model.OrderOnline_Role();

                    orderOnlineRole.Id = Convert.ToString(dr["ID"]);
                    orderOnlineRole.RoleName = Convert.ToString(dr["RoleName"]);
                    orderOnlineRole.RoleDescr = Convert.ToString(dr["RoleDescr"]);
                    orderOnlineRole.Rights = Convert.ToString(dr["Rights"]);
                    orderOnlineRole.RoleType = Convert.ToInt32(dr["RoleType"]);
                    orderOnlineRole.RoleShop = Convert.ToString(dr["RoleShop"]);
                    orderOnlineRole.ModelState = WMS.Model.Enum.ModelState.None;

                    orderOnlineRoleList.Add(orderOnlineRole);
                }

                // Close the dr, which will cause the connection to be closed as well.
                dr.Close();

                if (orderOnlineRoleList.Count > 0)
                {
                    return orderOnlineRoleList[0];
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        #endregion

        #region	公共方法

        /// <summary>
        /// 获取实体列表(指定数目和查询条件)
        /// </summary>
        /// <param name="topClause">指定实体数</param>
        /// <param name="whereClause">WHERE语句</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Role> Get(int topClause, string whereClause)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@TopClause", SqlDbType.Int,4),
				new SqlParameter("@WhereClause", SqlDbType.NVarChar,2000)
			};
            parameters[0].Value = topClause;
            parameters[1].Value = whereClause;

            try
            {
                List<WMS.Model.OrderOnline_Role> orderOnlineRoleList = new List<WMS.Model.OrderOnline_Role>();

                SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_Role_GetDynamic]", parameters);
                while (dr.Read())
                {
                    WMS.Model.OrderOnline_Role orderOnlineRole = new WMS.Model.OrderOnline_Role();

                    orderOnlineRole.Id = Convert.ToString(dr["ID"]);
                    orderOnlineRole.RoleName = Convert.ToString(dr["RoleName"]);
                    orderOnlineRole.RoleDescr = Convert.ToString(dr["RoleDescr"]);
                    orderOnlineRole.Rights = Convert.ToString(dr["Rights"]);
                    orderOnlineRole.RoleType = Convert.ToInt32(dr["RoleType"]);
                    orderOnlineRole.RoleShop = Convert.ToString(dr["RoleShop"]);
                    orderOnlineRole.ModelState = WMS.Model.Enum.ModelState.None;

                    orderOnlineRoleList.Add(orderOnlineRole);
                }

                // Close the dr, which will cause the connection to be closed as well.
                dr.Close();

                return orderOnlineRoleList;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 获取实体列表(指定查询条件)
        /// </summary>
        /// <param name="whereClause">WHERE语句</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Role> Get(string whereClause)
        {
            return Get(0, whereClause);
        }

        /// <summary>
        /// 获取分页实体列表
        /// </summary>
        /// <param name="whereClause">WHERE 语句</param>
        /// <param name="orderByClause">ORDER BY 语句</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小(每页显示的记录数)</param>
        /// <param name="totalRecords">记录总数</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Role> GetPaged(string whereClause, string orderBy, int pageIndex, int pageSize, out int totalRecords)
        {
            SqlParameter[] parameters = {
				new SqlParameter("@WhereClause", SqlDbType.NVarChar, 2000),
				new SqlParameter("@OrderBy", SqlDbType.NVarChar, 1000),
				new SqlParameter("@PageIndex", SqlDbType.Int, 4),
				new SqlParameter("@PageSize", SqlDbType.Int, 4)
			};
            parameters[0].Value = whereClause;
            parameters[1].Value = orderBy;
            parameters[2].Value = pageIndex;
            parameters[3].Value = pageSize;

            try
            {
                List<WMS.Model.OrderOnline_Role> orderOnlineRoleList = new List<WMS.Model.OrderOnline_Role>();

                SqlDataReader dr = DbHelperSQL.ExecuteStoredProcReader("[OrderOnline_Role_GetPaged]", parameters);
                while (dr.Read())
                {
                    WMS.Model.OrderOnline_Role orderOnlineRole = new WMS.Model.OrderOnline_Role();

                    orderOnlineRole.Id = Convert.ToString(dr["ID"]);
                    orderOnlineRole.RoleName = Convert.ToString(dr["RoleName"]);
                    orderOnlineRole.RoleDescr = Convert.ToString(dr["RoleDescr"]);
                    orderOnlineRole.Rights = Convert.ToString(dr["Rights"]);
                    orderOnlineRole.RoleType = Convert.ToInt32(dr["RoleType"]);
                    orderOnlineRole.RoleShop = Convert.ToString(dr["RoleShop"]);
                    orderOnlineRole.ModelState = WMS.Model.Enum.ModelState.None;

                    orderOnlineRoleList.Add(orderOnlineRole);
                }

                totalRecords = orderOnlineRoleList.Count;

                if (dr.NextResult())
                {
                    if (dr.Read())
                    {
                        totalRecords = Convert.ToInt32(dr[0]);
                    }
                }

                // Close the dr, which will cause the connection to be closed as well.
                dr.Close();

                return orderOnlineRoleList;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        #endregion

        #region 自定义方法

        /// <summary>
        /// 获取实体列表(根据角色类别)
        /// </summary>
        /// <param name="roleType">角色类别</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Role> GetByRoleType(int roleType)
        {
            return Get(String.Format("RoleType={0}", roleType));
        }

        public List<WMS.Model.OrderOnline_Role> GetByRoleType(int roleType, string roleShop)
        {
            StringBuilder whereClause = new StringBuilder();
            whereClause.AppendFormat("RoleType={0}", roleType);
            whereClause.AppendFormat(" AND RoleShop='{0}'", roleShop);
            return Get(whereClause.ToString());
        }

        /// <summary>
        /// 获取分页实体列表
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <param name="roleType">角色类别</param>
        /// <param name="roleShop">所属店铺</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小(每页显示的记录数)</param>
        /// <param name="totalRecords">记录总数</param>
        /// <returns></returns>
        public List<WMS.Model.OrderOnline_Role> GetPaged(string roleName, int roleType, string roleShop, int pageIndex, int pageSize, out int totalRecords)
        {
            StringBuilder whereClause = new StringBuilder();
            whereClause.AppendFormat("RoleType={0}", roleType);
            whereClause.AppendFormat(" AND RoleShop='{0}'", roleShop);

            if (!String.IsNullOrEmpty(roleName))
            {
                whereClause.AppendFormat(" AND RoleName LIKE '%{0}%'", roleName);
            }

            return GetPaged(whereClause.ToString(), String.Empty, pageIndex, pageSize, out totalRecords);
        }

        /// <summary>
        /// 判断角色名称是否存在
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <param name="roleType">角色类别</param>
        /// <param name="roleShop">所属店铺</param>
        /// <returns></returns>
        public bool Exists(string roleName, int roleType, string roleShop)
        {
            StringBuilder whereClause = new StringBuilder();
            whereClause.AppendFormat("RoleType={0}", roleType);
            whereClause.AppendFormat(" AND RoleShop='{0}'", roleShop);
            whereClause.AppendFormat(" AND RoleName = '{0}'", roleName);

            return Get(whereClause.ToString()).Count > 0;
        }

        #endregion
    }
}
